package com.sinping.qh.api.admin.common;

import com.sinping.qh.api.admin.base.BaseAdminEndPoint;
import com.sinping.qh.api.app.base.BaseAppEndPoint;
import com.sinping.qh.api.base.BaseEndPoint;
import com.sinping.qh.api.support.Const;
import com.sinping.qh.api.support.ConstEum;
import com.sinping.qh.api.support.ResDto;
import com.sinping.qh.dto.common.ApiUploadDTO;
import com.sinping.qh.dto.common.ApiUploadFileDto;
import com.sinping.qh.dto.common.CommonDTO;
import com.sinping.qh.dto.common.VersionUpdateDTO;
import com.sinping.qh.service.common.FileManagerService;
import com.sinping.qh.service.common.IVersionUpdateService;
import com.sinping.qh.utils.mapper.JsonMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 通用接口
 *
 * @author Tiansx
 * @create 2018-07-02 9:56
 **/
@Api(value = "通用部分", description = "通用部分API")
@RestController
@RequestMapping(value = Const.COMMON_URI, name = "公共API")
public class CommonEndPoint extends BaseEndPoint {
    @Autowired
    private FileManagerService commonService;

    @Autowired
    IVersionUpdateService iVersionUpdateService;

    /**
     * @param
     * @方法名称: images
     * @功能描述: 上传图片
     * @作者:杨云博
     * @创建时间:2018/1/3 20:01
     */
    @ApiOperation(value = "上传图片")
    @RequestMapping(value = "/images", name = "上传图片", method = RequestMethod.POST)
    public ResDto images(@ApiParam(name = "apiUploadDTO", value = " ", required = true) ApiUploadDTO apiUploadDTO) {
        ResDto resDto = new ResDto();
        //ApiUploadDTO apiUploadDTO = JsonMapper.defaultMapper().fromJson(msg,ApiUploadDTO.class);
        if (StringUtils.isBlank(apiUploadDTO.getRoleId())) {
            resDto = new ResDto(Const.PARAM_ERROR, "角色id不能为空");
            return resDto;
        }

//        if(StringUtils.isBlank(apiUploadDTO.getRoleType())){
//            resDto = new ResDto(Const.PARAM_ERROR,"文件类型不能为空");
//            return resDto;
//        }

        //调用上传service
        try {
            String imgId = commonService.saveImage(apiUploadDTO.getRoleId()
                    , apiUploadDTO.getUploadFileName().getOriginalFilename(), "1"
                    , apiUploadDTO.getUploadFileName().getBytes(), apiUploadDTO.getRemark());

            if (imgId != null) {
                Map<String, String> res = new HashMap<>();
                res.put("id", imgId);
                resDto = new ResDto(ConstEum.SUCCESS);
                resDto.setDatas(JsonMapper.defaultMapper().toJson(res));
                return resDto;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        //返回上传状态，成功返回图片id，失败返回错误信息
        resDto = new ResDto(Const.PARAM_ERROR, "上传失败");
        return resDto;
    }

    @ApiOperation(value = "上传文件")
    @RequestMapping(value = "/files", name = "上传文件", method = RequestMethod.POST)
    public ResDto uploadFile(@ApiParam(name = "apiUploadDTO", value = " ", required = true) ApiUploadDTO apiUploadDTO) {
        ResDto resDto = new ResDto();
        if (StringUtils.isBlank(apiUploadDTO.getRoleId())) {
            resDto = new ResDto(Const.PARAM_ERROR, "角色id不能为空");
            return resDto;
        }

//        if(StringUtils.isBlank(apiUploadDTO.getRoleType())){
//            resDto = new ResDto(Const.PARAM_ERROR,"文件类型不能为空");
//            return resDto;
//        }

        //调用上传service

        try {

            ApiUploadFileDto apiUploadFileDto=new ApiUploadFileDto();
            String fileId = commonService.saveFile(apiUploadDTO.getRoleId()
                    , apiUploadDTO.getUploadFileName().getOriginalFilename(), "2"
                    , apiUploadDTO.getUploadFileName().getBytes(), apiUploadDTO.getRemark());

            if (fileId != null) {
                String fileUrl = commonService.getFileOrImageUrlById(fileId);

                apiUploadFileDto.setId(fileId);
                apiUploadFileDto.setFileUrl(fileUrl);
            }

            if (fileId != null) {
                resDto = new ResDto(ConstEum.SUCCESS);
                resDto.setDatas(JsonMapper.defaultMapper().toJson(apiUploadFileDto));
                return resDto;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        //返回上传状态，成功返回图片id，失败返回错误信息
        resDto = new ResDto(Const.PARAM_ERROR, "上传失败");
        return resDto;
    }

    /**
     * @param id return String
     * @方法名称: downloadImages
     * @功能描述: 下载图片
     * @作者:杨云博
     * @创建时间:2018/1/3 20:03
     */
    @ApiOperation(value = "获取图片链接")
    @RequestMapping(value = "/images/{id}", name = "获取图片链接", method = RequestMethod.GET)
    public ResDto downloadImages(@PathVariable String id) {
        //验证非空
        if (StringUtils.isBlank(id)) {
            return null;
        }
        //调用下载图片service
        String url = commonService.getImageUrlById(id);
        if (StringUtils.isNotBlank(url)) {
            ResDto resDto = new ResDto(ConstEum.SUCCESS);
            resDto.setDatas(url);
            return resDto;
        }


        return new ResDto(ConstEum.FAIL);

    }

    @ApiOperation(value = "获取文件链接")
    @RequestMapping(value = "/files/{id}", name = "获取文件链接", method = RequestMethod.GET)
    public ResDto downloadFiles(@PathVariable String id) {
        //验证非空
        if (StringUtils.isBlank(id)) {
            return null;
        }
        //调用下载文件service
        String url = commonService.getImageUrlById(id);
        if (StringUtils.isNotBlank(url)) {
            ResDto resDto = new ResDto(ConstEum.SUCCESS);
            resDto.setDatas(url);
            return resDto;
        }


        return new ResDto(ConstEum.FAIL);

    }
    @ApiOperation(value = "检查版本更新")
    @RequestMapping(value = "/check_version", name = "检查版本更新", method = RequestMethod.GET)
    public ResDto checkVersionUpdate() {

        ResDto resDto = new ResDto(ConstEum.SUCCESS);

        CommonDTO commonDTO =new CommonDTO();
        commonDTO.setPlatform(getRequestPlatform());

        VersionUpdateDTO versionUpdateDTO=
                iVersionUpdateService.versionUpdate(commonDTO);

        checkNull(versionUpdateDTO,ConstEum.FAIL);

        resDto.setMsg(ConstEum.SUCCESS.getMessage());
        resDto.setDatas(JsonMapper.defaultMapper().toJson(versionUpdateDTO));

        return resDto;

    }

//    @ApiOperation(value = "删除图片")
//    @RequestMapping(value = "/images/{id}", name = "删除图片", method = RequestMethod.DELETE)
//    public ResDto deleteImages(@PathVariable String id) {
//        //验证非空
//        if (StringUtils.isBlank(id)) {
//            return null;
//        }
//        return commonService.deleteById(id);
//
//    }
//
//    @ApiOperation(value = "删除文件")
//    @RequestMapping(value = "/files/{id}", name = "删除文件", method = RequestMethod.DELETE)
//    public ResDto deleteFiles(@PathVariable String id) {
//        //验证非空
//        if (StringUtils.isBlank(id)) {
//            return null;
//        }
//        return commonService.deleteById(id);
//
//    }
}
